﻿using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Linq;
using System.Linq;
using ExplorerInfrastructure;

namespace BaseTypesLib.Model
{
    [DataContract(IsReference = true)]
    public class BitmapField:Field
    {
        private int hSpace, vSpace;
        private string bitmap;

        public BitmapField()
        {
            Focusable = false;
        }

        [DataMember]
        public int HorizontalSpace
        {
            get { return hSpace; }
            set
            {
                if (hSpace != value)
                {
                    hSpace = value;
                    OnPropertyChanged("HorizontalSpace");
                }
            }
        }
        [DataMember]
        public int VerticalSpace
        {
            get { return vSpace; }
            set
            {
                if (vSpace != value)
                {
                    vSpace = value;
                    OnPropertyChanged("VerticalSpace");
                }
            }
        }
        [DataMember]
        public string Bitmap
        {
            get { return bitmap; }
            set
            {
                if (bitmap != value)
                {
                    bitmap = value;
                    OnPropertyChanged("Bitmap");
                }
            }
        }

        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();
            if (VerticalSpace > 0)
                el.SetAttributeValue("VerticalSpace", VerticalSpace);
            if (HorizontalSpace > 0)
                el.SetAttributeValue("HorizontalSpace", HorizontalSpace);
            if (Bitmap != null && Bitmap.Trim().Length > 0)
                el.SetAttributeValue("Bitmap", Bitmap.Trim());
            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var bmp = elem.Attributes("Bitmap").FirstOrDefault();
            if (bmp != null)
                Bitmap = bmp.Value;
            var hs = elem.Attributes("HorizontalSpace").FirstOrDefault();
            if (hs != null)
                HorizontalSpace = (int)hs;
            var vs = elem.Attributes("VerticalSpace").FirstOrDefault();
            if (vs != null)
                VerticalSpace = (int)vs;
        }
    }
}
